﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace VisualTrajectory
{
    public partial class MainForm : Form
    {
        private TrajectoryCollection m_Trajs;
        private float m_fTrajViewZoomFactor = 1.0f;

        public MainForm()
        {
            InitializeComponent();
            this.trajViewPanel.SetAutoSetViewPort(false);
            this.trajViewPanel.AutoSetViewPort();
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog fileDlg = new OpenFileDialog();
            DialogResult dr = fileDlg.ShowDialog();
            if (dr == DialogResult.OK)
            {
                m_Trajs = new TrajectoryCollection();
                int nRet = m_Trajs.LoadFromTxtFile(fileDlg.FileName);
                if (nRet < 0)
                {
                    MessageBox.Show("Load trajectories error!");
                }
                else
                {
                    // Set the trajectories to the panel
                    this.trajViewPanel.SetTrajectories(m_Trajs);
                    this.trajViewPanel.Invalidate();
                }
            }
        }

        private void TrajViewZoomInButton_Click(object sender, EventArgs e)
        {
            m_fTrajViewZoomFactor = 1.2f;
            Size viewSize = this.trajViewPanel.GetViewSize();
            viewSize.Width = (int)(((float)viewSize.Width) * m_fTrajViewZoomFactor);
            viewSize.Height = (int)(((float)viewSize.Height) * m_fTrajViewZoomFactor);
            this.trajViewPanel.SetViewSize(viewSize);
            this.trajViewPanel.SetAutoSetViewPort(false);
            this.trajViewPanel.ReProject();
            this.trajViewPanel.Invalidate();
        }

        private void TrajViewZommOutButton_Click(object sender, EventArgs e)
        {
            m_fTrajViewZoomFactor = 0.8f;
            Size viewSize = this.trajViewPanel.GetViewSize();
            viewSize.Width = (int)(((float)viewSize.Width) * m_fTrajViewZoomFactor);
            viewSize.Height = (int)(((float)viewSize.Height) * m_fTrajViewZoomFactor);
            this.trajViewPanel.SetViewSize(viewSize);
            this.trajViewPanel.SetAutoSetViewPort(false);
            this.trajViewPanel.ReProject();
            this.trajViewPanel.Invalidate();
        }

        private void closeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void SelectTrajModeCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            bool bSelectMode = this.SelectTrajModeCheckBox.Checked;
            this.trajViewPanel.SetTrajectorySelectMode(bSelectMode);
        }

        private void ShowSelectedTrajInfoButton_Click(object sender, EventArgs e)
        {
            int nSelectedTrajIndex = this.trajViewPanel.GetSelectedTrajectory();
            if (nSelectedTrajIndex == -1)
                return;

            Trajectory selectedTraj = m_Trajs.GetTrajectoryByIndex(nSelectedTrajIndex);
            StringBuilder sb = new StringBuilder();
            sb.Append("Trajectory Name : " + selectedTraj.GetName()+ " ID : "+nSelectedTrajIndex);
            MessageBox.Show(sb.ToString());
        }

        private void selectByNameToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TextBoxDialog inputNameDlg = new TextBoxDialog();
            DialogResult result = inputNameDlg.ShowDialog();
            if (result == DialogResult.OK)
            {
                string trajName = inputNameDlg.GetText();
                int nSelectedTrajIndex = m_Trajs.GetTrajectoryIndexByName(trajName);
                if (nSelectedTrajIndex == -1)
                {
                    MessageBox.Show("Cannot find the trajectory : " + trajName);
                    return;
                }
                this.trajViewPanel.SetSelectedTrajectory(nSelectedTrajIndex);
                this.trajViewPanel.Invalidate();
            }
        }

        private void selectByIDToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TextBoxDialog inputNameDlg = new TextBoxDialog();
            DialogResult result = inputNameDlg.ShowDialog();
            if (result == DialogResult.OK)
            {
                string strTrajIndex = inputNameDlg.GetText();
                int nSelectedTrajIndex = int.Parse(strTrajIndex);
                if (nSelectedTrajIndex < 0)
                {
                    MessageBox.Show("Cannot find the trajectory : " + strTrajIndex);
                    return;
                }
                this.trajViewPanel.SetSelectedTrajectory(nSelectedTrajIndex);
                this.trajViewPanel.Invalidate();
            }
        }

        private void editHighlightedToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TextBoxDialog inputNameDlg = new TextBoxDialog();
            DialogResult result = inputNameDlg.ShowDialog();
            if (result == DialogResult.OK)
            {
                string strTrajIndexesLine = inputNameDlg.GetText();
                string[] strTrajIndexes = strTrajIndexesLine.Split(new char[] { ',', ' ', '\t', '\r', '\n' });
                List<int> highlightindexes = new List<int>();
                for (int i = 0; i < strTrajIndexes.Length; i++)
                {
                    if (strTrajIndexes[i].Length < 1)
                        continue;
                    int nTrajIndex = int.Parse(strTrajIndexes[i]);
                    highlightindexes.Add(nTrajIndex);
                }
                this.trajViewPanel.SetHighlightedTrajectories(highlightindexes);
                this.trajViewPanel.Invalidate();
            }
        }

        private void DeleteTrajButton_Click(object sender, EventArgs e)
        {
            if (this.trajViewPanel.GetSelectedTrajectory() != -1)
            {
                int nSelectedIndex = this.trajViewPanel.GetSelectedTrajectory();
                m_Trajs.Delete(nSelectedIndex);
                this.trajViewPanel.SetTrajectories(m_Trajs);
                this.trajViewPanel.SetSelectedTrajectory(-1);
                this.trajViewPanel.Invalidate();
            }
        }

        private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog dlg = new SaveFileDialog();
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                string filepath = dlg.FileName;
                FileStream fileStream = new FileStream(filepath, FileMode.Create);
                StreamWriter writer = new StreamWriter(fileStream);
                m_Trajs.WriteToStream(writer);
                writer.Close();
                fileStream.Close();
                MessageBox.Show(filepath + " save ok!");
            }
        }

        private void summaryToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int nTotalNumTraj = m_Trajs.Count();
            int nTotalPoint = 0;
            for (int i = 0; i < m_Trajs.Count(); i++)
            {
                nTotalPoint += m_Trajs.GetTrajectoryByIndex(i).GetPointCount();
            }
            MessageBox.Show("Total " + nTotalNumTraj + " Trajectories \nTotal " + nTotalPoint + " points");
        }

    }
}
